-- UISkillUpgrade
-- Created by chengb Mar/31/2015
-- 技能升级界面

require "game/ui/form/common/UIInvisibleForm"

UISkillUpgrade = class("UISkillUpgrade", function()
    return cc.Layer:create();
end);

function UISkillUpgrade.create(careerId)
    return UISkillUpgrade.new(careerId);
end

-- 属性半透明度
local ATTRIB_OPACITY_VALUE = 100;

-- 内部函数声明
local resize;

function UISkillUpgrade:ctor(careerId)
    -- 初始化
    self:setName("UISkillUpgrade");
    local node = cc.CSLoader:createNode("layout/hero/SkillUpgrade.csb");
    self:addChild(node);
    self.node = node;

    self.careerId = careerId;

    local pet = ME.user:getActivePet();
    self.pet = pet;

    -- 需要显示在头部菜单上面
    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    resize(node);
end

-- 适配
function resize(node)
   -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 重绘
function UISkillUpgrade:redraw()
    -- 重绘技能框
    self:redrawSkillDetail();

    -- 显示当前探索点
    local spNode = findChildByName(self.node, "BT/sp_node");
    local iconImg = findChildByName(spNode, "icon");
    local icon = FieldsM.query("search_point", "icon");
    iconImg:loadTexture(getSmallAttribIconPath(icon));
    local amountLabel = findChildByName(spNode, "amount");
    TextStyleM.setTextStyle(amountLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BRIGHT_YELLOW, true, -2);
    amountLabel:setString(tonumber(ME.user:queryAttrib("search_point")));

    -- 重绘返回按钮
    local btnBack = findChildByName(self.node, "BT/btn_back");
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 重绘升级按钮
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(getLocStr("btn_text_upgrade"));

    -- 如果称号已满级，则升级按钮置灰
    if CareerM.isCareerMaxLevel(self.pet, self.careerId) then
        btnOk:setTouchEnabled(false);
        btnOk:setBright(false);
    else
        btnOk:setTouchEnabled(true);
        btnOk:setBright(true);
    end
end

-- 重绘技能详细信息
function UISkillUpgrade:redrawSkillDetail()
    local careerId = self.careerId;
    local careerInfo = CareerM.getCareerInfo(careerId);

    -- 显示标题（职业名称）
    local titleLabel = findChildByName(self.node, "CT/title_node/title");
    titleLabel:setString(careerInfo["name"]);
    TextStyleM.setArtWordTitleStyle(titleLabel);

    -- 显示当前职业等级
    local level = CareerM.getCareerLevel(self.pet, careerId);
    local starNode = findChildByName(self.node, "CT/title_node/star_node");
    local curStar  = level;
    local maxStar  = careerInfo["max_level"];
    local lightStarPath = "images/ui/symbol/star_large.png";
    local darkStarPath  = "images/ui/symbol/star_large2.png";
    local layer = createStarLayerEx(curStar, maxStar, lightStarPath, darkStarPath);
    if layer then
        starNode:removeAllChildren();
        starNode:addChild(layer);
    end

    -- 显示当前等级的职业效果
    self:redrawCurrEffect();

    -- 显示bar中的描述
    local descLabel = findChildByName(self.node, "CT/bar/desc");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    descLabel:setString(getLocStr("next_level_effect"));

    -- 显示升级效果
    self:redrawImproveEffect();

    -- 显示探索点消耗
    local costInfo = CareerM.calcUpgradeCost(self.pet.classId, careerId);
    local needSp = costInfo["search_point"];
    local costStr = string.format(getLocStr("cost_sp_format2"), needSp);

    if CareerM.isCareerMaxLevel(self.pet, self.careerId) then
        -- 已经满级了，消耗特殊显示
        costStr = string.format(getLocStr("cost_sp_format3"));
    end

    local costLabel = findChildByName(self.node, "CT/cost_node/cost_label");
    TextStyleM.setTextStyle(costLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    applyString(costLabel, costStr);

    -- 显示背景称号图标
    -- 放大、半透明显示
    local bgIconImg = findChildByName(self.node, "CT/bg_icon");
    bgIconImg:loadTexture(getCareerIconPath(careerInfo["icon"]));
    bgIconImg:setScale(1.8);

    -- 如果已经满级，透明度会高
    if CareerM.isCareerMaxLevel(self.pet, self.careerId) then
        bgIconImg:setOpacity(100);
    else
        bgIconImg:setOpacity(50);
    end
end

-- 获取基础属性值
function UISkillUpgrade:getBasePropValue(baseProps, field)
    for _, arr in pairs(baseProps) do
        if arr[2] == field then
            return arr[3];
        end
    end
    return 0;
end

-- 显示当前等级的职业效果
function UISkillUpgrade:redrawCurrEffect()
    -- 获取基础属性值
    local baseProps = CareerM.getFeatureBaseProps(self.pet.classId, self.careerId, true);

    -- 显示属性
    local function showAttribValue(value, valueLabel, nameLabel, parentNode, effectNode)
        TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BEIGE, false, -1);
        TextStyleM.setTextStyle(valueLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BEIGE, false, -1);
        if value <= 0 then
            -- 半透明显示
            valueLabel:setString("");
            parentNode:setOpacity(ATTRIB_OPACITY_VALUE);
            effectNode:removeAllChildren();
        else
            valueLabel:setString("+" .. value);
            parentNode:setOpacity(255);

            -- 播放闪光特效
            playAttribSparkEffect(effectNode, 1133);
        end
    end

    -- 显示基础属性
    local basePropNode = findChildByName(self.node, "CT/base_prop_node");

    -- 攻击
    local attackNode = findChildByName(basePropNode, "attack_node");
    local nameLabel  = findChildByName(basePropNode, "attack_node/name");
    local valueLabel = findChildByName(basePropNode, "attack_node/value");
    local effectNode = findChildByName(basePropNode, "attack_node/effect_node");
    nameLabel:setString(getLocStr("attack"));
    local value = self:getBasePropValue(baseProps, "attack");
    showAttribValue(value, valueLabel, nameLabel, attackNode, effectNode);

    -- 生命
    local hpNode = findChildByName(basePropNode, "hp_node");
    nameLabel  = findChildByName(basePropNode, "hp_node/name");
    valueLabel = findChildByName(basePropNode, "hp_node/value");
    effectNode = findChildByName(basePropNode, "hp_node/effect_node");
    nameLabel:setString(getLocStr("hp"));
    local value = self:getBasePropValue(baseProps, "max_hp");
    showAttribValue(value, valueLabel, nameLabel, hpNode, effectNode);

    -- 魔力
    local magicNode = findChildByName(basePropNode, "magic_node");
    nameLabel  = findChildByName(basePropNode, "magic_node/name");
    valueLabel = findChildByName(basePropNode, "magic_node/value");
    effectNode = findChildByName(basePropNode, "magic_node/effect_node");
    nameLabel:setString(getLocStr("magic"));
    local value = self:getBasePropValue(baseProps, "magic");
    showAttribValue(value, valueLabel, nameLabel, magicNode, effectNode);

    -- 魔法
    local mpNode = findChildByName(basePropNode, "mp_node");
    nameLabel  = findChildByName(basePropNode, "mp_node/name");
    valueLabel = findChildByName(basePropNode, "mp_node/value");
    effectNode = findChildByName(basePropNode, "mp_node/effect_node");
    nameLabel:setString(getLocStr("mp"));
    local value = self:getBasePropValue(baseProps, "max_mp");
    showAttribValue(value, valueLabel, nameLabel, mpNode, effectNode)

    -- 显示职业特性描述
    local propLabel = findChildByName(self.node, "CT/prop_node/cur_prop_desc");
    local propStr = CareerM.getFeatureDesc(self.pet.classId, self.careerId, true);
    TextStyleM.setTextStyle(propLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    applyString(propLabel, propStr);
end

-- 显示升级效果
function UISkillUpgrade:redrawImproveEffect()
    local upperTipLabel = findChildByName(self.node, "CT/level_upper_tip");
    local propLabel = findChildByName(self.node, "CT/impove_prop_desc");
    local kerning = 0;
    if getLang() == "fr" then
        kerning = -1;
    end

    -- 如果已经满级
    if CareerM.isCareerMaxLevel(self.pet, self.careerId) then
        -- 显示满级提示信息
        upperTipLabel:setVisible(true);
        TextStyleM.setTextStyle(upperTipLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_GRAY, nil, nil, nil, nil, nil, TextStyleM.TEXT_SIZE_MIDDLE);
        upperTipLabel:setString(getLocStr("career_level_upper_tip"));

        propLabel:setVisible(false);
        return;
    end

    upperTipLabel:setVisible(false);
    propLabel:setVisible(true);

    -- 获取下一等级基础属性值
    local nextBaseProps = CareerM.getFeatureBaseProps(self.pet.classId, self.careerId, false);

    local content = "";
    local len = #nextBaseProps
    if len > 0 then
        for i, arr in pairs(nextBaseProps) do
            local field = arr[2];
            local value = arr[3];
            local fieldName = FieldsM.getFieldName(field, "name");
            content = content .. fieldName .. "+" .. value;
            if i < len then
                content = content .. getLocStr("pause_mark");
            end
        end
    end

    content = content .. "\n\n";

    -- 显示其他属性描述
    local propStr = CareerM.getFeatureDesc(self.pet.classId, self.careerId, false);
    propStr = extractString(propStr);
    content = content .. propStr;
    TextStyleM.setTextStyle(propLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY, nil, kerning, nil, nil, nil, TextStyleM.TEXT_SIZE_SMALL);
    propLabel:setString(content);
end

-- 注册点击事件
function UISkillUpgrade:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UISkillUpgrade");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);

    -- 注册学习按钮点击事件
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            if CareerM.isCareerMaxLevel(self.pet, self.careerId) then
                -- 该职业已满级
                alert(getLocStr("career_level_upper"));
                return;
            end

            -- 判断能否升级
            local ret = CareerM.canUpgrade(self.pet.classId, self.careerId);
            if ret ~= true then
                if type(ret) == "string" then
                    alert(ret);
                end
                return;
            end

            -- 升级
            DungeonActionM.go("upgrade_career", self.careerId);
        end
    end
    btnOk:addTouchEventListener(onOkClick);
end

-- 注册事件处理回调函数
function UISkillUpgrade:registerEventCallback()
    EventMgr.register("UISkillUpgrade", event.UPGRADE_CAREER, function(args)
        UIMgr.getCurrentScene():removeFormByName("UISkillUpgrade");
    end);

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            EventMgr.removeAll("UISkillUpgrade");
        end
    end);
end













